﻿using System;
using System.Windows;
using AppfabricAdministrator.Entities;

namespace AdminMainUI
{
    /// <summary>
    /// Interaction logic for HostConfig.xaml
    /// </summary>
    public partial class HostConfigWindow : Window
    {
        private HostConfig config = null;
        public HostConfigWindow()
        {
            InitializeComponent();
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
        }

        public HostConfigWindow(HostConfig _config)
        {
            InitializeComponent();
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;

            config = _config;
            this.Title = String.Format(this.Title, _config.HostName);
            txtHostName.Text=_config.HostName;
            txtArbitrationPort.Text = _config.ArbitrationPortNumber.ToString();
            txtReplicationPort.Text = _config.ReplicationPortNumber.ToString();
            txtCachePort.Text = _config.CachePort.ToString();
            txtClusterPort.Text = _config.ClusterPortNumber.ToString();
            txtHighWaterMark.Text = _config.HighWatermark.ToString();
            txtLowWaterMark.Text = _config.LowWatermark.ToString();
            txtServiceName.Text = _config.ServiceName;
            txtSize.Text = _config.CacheSize.ToString();
            chkIsLeadHost.IsChecked = Boolean.Parse(_config.IsLeadHost);
        }

        public HostConfig SetHostConfig()
        {
            this.ShowDialog();
            return config;
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            config = null;
            this.Close();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            config.HostName = txtHostName.Text;
            config.ArbitrationPortNumber=Convert.ToInt32(txtArbitrationPort.Text);
            config.ReplicationPortNumber = Convert.ToInt32(txtReplicationPort.Text);
            config.CachePort=Convert.ToInt32(txtCachePort.Text);
            config.ClusterPortNumber=Convert.ToInt32(txtClusterPort.Text);
            config.HighWatermark=Convert.ToInt32(txtHighWaterMark.Text);
            config.LowWatermark=Convert.ToInt32(txtLowWaterMark.Text);
            config.CacheSize=Convert.ToInt64(txtSize.Text);
            config.IsLeadHost = chkIsLeadHost.IsChecked == true ? "true" : "false";
            config.RefreshNow = true;
            this.Close();
        }


    }
}
